<?php

namespace Swof\Routing;

class RouteAction
{
    /**
     * Note: 解析
     *
     * @param $uri
     * @param $action
     * @return array|\Closure[]|string[]
     * @author: Zhao Zhiqiang
     * @since: 2021/5/24 17:15
     */
    public static function parse($uri, $action): array
    {
        if (is_null($action)) {
            return static::missingAction($uri);
        }

        return ! is_array($action) ? ['uses' => $action] : [
            'uses' => $action[0].'@'.$action[1],
            'controller' => $action[0].'@'.$action[1],
        ];
    }

    /**
     * Get an action for a route that has no action.
     *
     * @param  string  $uri
     * @return array
     *
     * @throws \LogicException
     */
    protected static function missingAction($uri)
    {
        return ['uses' => function () use ($uri) {
            throw new \LogicException("Route for [{$uri}] has no action.");
        }];
    }
}
